﻿function Install-WacServer2013
{
    <# 
     .Synopsis
      Launches the Office Web Applications Server 2013 (WAC Server) Installer.

     .Description
      This script launches the Office Web Applications Server 2013 (WAC Server) Installer

     .Parameter PathToWacServerInstallFiles
      The full file system path where the installation source files for Office Web Applications Server 2013 are located. 
      The default value for this parameter is 'C:\ProgramData\SharePoint Total Automation Toolkit\Software\10-WacServer2013'      

     .Example
       Install-WacServer2013 

     .Example
       Install-WacServer2013 -PathToWacServerInstallFiles 'C:\ProgramData\SharePoint Total Automation Toolkit\Software\10-WacServer2013'

    #>
    [CmdletBinding(DefaultParametersetName="CustomPaths")]
    param([parameter(ParameterSetName = "CustomPaths", 
                     Mandatory = $false,
                     HelpMessage = "The full file system path where the installation source files for SharePoint 2013 are located. The default value for this parameter is 'C:\ProgramData\SharePoint Total Automation Toolkit\Software\04-SharePointServer2013'")]
                     [string]$PathToWacServerInstallFiles = ($env:ProgramData + "\SharePoint Total Automation Toolkit\Software\10-WacServer2013"))

    switch ($PSCmdlet.ParameterSetName)
    {
        "CustomPaths"
        {
        }    
        default
        {
        }    
    }

	$rebootStatus = Get-PendingReboot
	if ($rebootStatus.RebootPending -eq $true)
	{
		Write-Host "NOTE: A system restart is required. Please restart the system prior to continuing with the installation." -ForegroundColor Yellow
		Write-Host "      When the system restarts please rerun this script until you no longer receive a restart request." -ForegroundColor Yellow
		throw 
	}

    # Initialize Progress Variables
    [int]$progressBarId = 100
    $startTime = [System.DateTime]::Now

    Write-Host
    Write-Host "Beginning the Office Web Applications Server 2013 installation."

        [System.Xml.XmlDocument]$currentConfigXml = 
        @"
<Configuration>
	<Logging Type="verbose" Path="%temp%" Template="Wac Server Setup(*).log"/>
	<Display Level="none" CompletionNotice="no" />
	<Setting Id="USINGUIINSTALLMODE" Value="0"/>
	<Setting Id="SETUPTYPE" Value="CLEAN_INSTALL"/>
	<Setting Id="SETUP_REBOOT" Value="Never"/>
</Configuration>
"@

    # Save the newly generated config.xml file to the local temp directory
    $configXmlName = ("WacServerconfig_{0}.xml" -f (Get-Date -Format 'yyyy_MMM_dd_HH_mm_ss') )
    $configXmlPath = Join-Path $env:temp $configXmlName
    $currentConfigXml.Save($configXmlPath)


    #region Task 1

    # Launch the Office Web Applications Server 2013 Prerequisite Installer
    $setupExe = ($PathToWacServerInstallFiles + "\setup.exe")
    $setupArgs = ("/config {0}" -f $configXmlPath)
    $setupExeInstance = [System.Diagnostics.Process]::Start($setupExe, $setupArgs)
    $step = 5
    $value = 0

    while (-not ($setupExeInstance.HasExited)){
        $currentRuntime = [System.DateTime]::Now - $startTime
        Write-Progress -Id $progressBarId -Activity "Installing Office Web Applications Server 2013" -PercentComplete ($value+=$step) -CurrentOperation "Executing the SharePoint Installation Wizard" -Status ("The current operation has been running for {0}:{1}:{2}" -f $currentRuntime.Hours.ToString("00"), $currentRuntime.Minutes.ToString("00"), $currentRuntime.Seconds.ToString("00"))
     
        if ($value -ge 100) { $value = 0}

        Start-Sleep -Seconds 1
    }

    if ($setupExeInstance.ExitCode -eq 0)
    {
        Write-Host "     Office Web Applications Server 2013 Successfully Installed"
    }
    else
    {
		$exitCode = $setupExeInstance.ExitCode
        Write-Error "     Installation of Office Web Applications Server 2013 Failed with exit code ($exitCode)"
        throw
    }

    #endregion

    Write-Progress -Id $progressBarId -Activity "Installing Office Web Applications Server 2013" -Status "Finishing" -PercentComplete 100 -CurrentOperation "" -Completed

    Write-Host "Office Web Applications Server 2013 installation complete."
    Write-Host
   
}